#include "fuel_cell_infos.h"
#include "types.h"
#include "main_matrix.h"
//~ #include "mpi_tools.h"
#include "params.h"

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>

void ExportMaterialsMap(FILE* fp);
void ExportNbNodesLists(FILE* fp);
void ExportNbNodesList(FILE* fp, ListInt* lst);
void ExportFuelCellMap(FILE* fp);

void BuildLegendFile(){
	FILE* fp = fopen("result/legend.txt", "w");

    ExportMaterialsMap(fp);
    ExportNbNodesLists(fp);
    ExportFuelCellMap(fp);

	fclose(fp);
}

void ExportMaterialsMap(FILE* fp){
    fprintf(fp, "<MatMap>\n");

    for(int i=0; i<MAT_END; i++)
        fprintf(fp, "matnum=%d, name=%s\n", i, tMaterial[i].name);

    fprintf(fp, "</MatMap>\n");
}

void ExportNbNodesLists(FILE* fp){
    ExportNbNodesList(fp, blockNbNodesX);
    ExportNbNodesList(fp, blockNbNodesY);
    ExportNbNodesList(fp, blockNbNodesZ);
}

void ExportNbNodesList(FILE* fp, ListInt* lst){
    fprintf(fp, "<ListInt>\nname=%s\nsize=%d\nlist=", lst->name, lst->size);
    for(int i=0; i<lst->size; i++){
        fprintf(fp, "%d", lst->list[i]);
        if( i!=lst->size - 1 )
            fprintf(fp, ", ");
    }
    fprintf(fp, "\n");
    fprintf(fp, "</ListInt>\n");
}

void ExportFuelCellMap(FILE* fp){
    fprintf(fp, "<FuelCellMap>\ndata=");

    for(int z=0; z<nbNodes.z; z++){
        for(int y=0; y<nbNodes.y; y++){
            for(int x=0; x<nbNodes.x; x++){
                fprintf(fp, "%d", fuelCellMap[x][y][z]);
                if( x != nbNodes.x - 1 || y != nbNodes.y - 1 || z != nbNodes.z - 1)
                    fprintf(fp, ",");
            }
        }
    }
    fprintf(fp, "\n</FuelCellMap>");
}

